<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace utils\Tools;

/**
 * @title HTTP请求
 */
class Http {
    
    
    

    /**
     * @title http请求
     * @param type $url
     * @return type
     */
    public static function https_request($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        if (curl_errno($curl)) {
            return 'ERROR ' . curl_error($curl);
        }
        curl_close($curl);
        return $data;
    }

    /**
     * @title curl请求指定url
     * @param $url
     * @param array $data
     * @return mixed
     */
    public static function curl($url, $data = []) {
        // 处理get数据
        if (!empty($data)) {
            $url = $url . '?' . http_build_query($data);
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //这个是重点。
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }
    
    
    /**
     * @title 下载资源 
     * @param type $url
     * @param type $filename
     */
    public static function _download($url, $filename) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        $file = curl_exec($ch);
        curl_close($ch);

        //$filename = pathinfo($url, PATHINFO_BASENAME);
        $resource = fopen($filename, 'a');

        fwrite($resource, $file);
        fclose($resource);
    }

}
